/*
 * EPoll.cpp
 *
 *  Created on: Nov 28, 2014
 *      Author: toti
 */

#include <Connectivity/EPoll.hpp>


EPoll::EPoll(uint32_t bufferSize){

	this->bufferSize = bufferSize;

	efd = epoll_create1(0);
	if(efd == -1){
		std::cerr << "epoll_create1()" << std::endl;
		exit(1);
	}
	eventBuffer = (struct epoll_event*)malloc(sizeof(struct epoll_event) * bufferSize);

}

void EPoll::addFd(int fd, void *ptr){

	int s;
	struct epoll_event epollEvent;

	epollEvent.data.ptr = ptr;
	epollEvent.events = EPOLLIN;
	s = epoll_ctl(efd, EPOLL_CTL_ADD, fd, &epollEvent);

	if(s == -1){
		std::cerr << "epoll_ctl()" << std::endl;
	}

}

void EPoll::remFd(int fd){

	epoll_ctl(efd, EPOLL_CTL_DEL, fd, 0);

}

int EPoll::wait(){

	return epoll_wait(efd, eventBuffer, bufferSize, -1);

}

struct epoll_event *EPoll::getEventBuffer(uint32_t i){

	if(i < bufferSize){

		return &eventBuffer[i];

	}

	return 0;

}
